<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
     <el-form-item label="姓名" prop="sportsmanId">
                 <el-select
                   v-model="queryParams.sportsmanId"
                   placeholder="请选择姓名"
                   clearable
                   size="small"
                   style="width: 240px"
                 >
                   <el-option
                     v-for="dict in sportsmanNameList"
                     :key="dict.id"
                     :label="dict.name"
                     :value="dict.id"
                   />
                 </el-select>
               </el-form-item>
      <el-form-item label="赛事名称" prop="matchName">
        <el-input
          v-model="queryParams.matchName"
          placeholder="请输入赛事名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="比赛项目" prop="event">
        <el-input
          v-model="queryParams.event"
          placeholder="请输入比赛项目"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['training:sportsmanMatch:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['training:sportsmanMatch:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="sportsmanMatchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="sportsmanName" />
      <el-table-column label="赛事名称" align="center" prop="matchName" />
      <el-table-column label="赛事时间" align="center" prop="matchTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.matchTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="比赛地点" align="center" prop="venue" />
      <el-table-column label="比赛项目" align="center" prop="event" />
      <el-table-column label="成绩" align="center" prop="achievement" />
      <el-table-column label="赛事简介" align="center" prop="memo" />
      <el-table-column label="创建时间" align="center" prop="ctime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ctime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['training:sportsmanMatch:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['training:sportsmanMatch:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改赛事成绩对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="姓名" prop="sportsmanId">
            <el-select
              v-model="form.sportsmanId"
              placeholder="请选择姓名"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in sportsmanNameList"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        <el-form-item label="赛事名称" prop="matchName">
          <el-input v-model="form.matchName" placeholder="请输入赛事名称" />
        </el-form-item>
        <el-form-item label="赛事时间" prop="matchTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.matchTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择赛事时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="比赛地点" prop="venue">
          <el-input v-model="form.venue" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="比赛项目" prop="event">
          <el-input v-model="form.event" placeholder="请输入比赛项目" />
        </el-form-item>
        <el-form-item label="成绩" prop="achievement">
          <el-input v-model="form.achievement" placeholder="请输入成绩" />
        </el-form-item>
        <el-form-item label="赛事简介" prop="memo">
          <el-input v-model="form.memo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="上传" prop="imgUrl" required>
          <el-upload ref="imgUrl" :file-list="evidencefileList" :action="evidenceAction" multiple
                     :before-upload="evidenceBeforeUpload" :on-success="evidenceUploadSuccess" list-type="picture" accept="image/*">
            <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSportsmanMatch, listSportsmanName, getSportsmanMatch, delSportsmanMatch, addSportsmanMatch, updateSportsmanMatch, exportSportsmanMatch } from "@/api/training/sportsmanMatch";

export default {
  name: "SportsmanMatch",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // sportsmanMatch表格数据
      sportsmanMatchList: [],
      //姓名下拉框数组
      sportsmanNameList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sportsmanId: undefined,
        sportsmanName:undefined,
        matchName: undefined,
        matchTime: undefined,
        venue: undefined,
        event: undefined,
        achievement: undefined,
        memo: undefined,
        ctime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sportsmanId: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        matchName: [
          { required: true, message: "赛事名称不能为空", trigger: "blur" }
        ],
      },
      evidenceAction: '/api/common/upload',
      evidencefileList: [],
    };
  },
  created() {
    this.getList();
    this.getNameList();
  },
  methods: {
    /** 查询sportsmanMatch列表 */
    getList() {
      this.loading = true;
      listSportsmanMatch(this.queryParams).then(response => {
        this.sportsmanMatchList = response.rows;
        console.log(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },

    //赛事姓名集合
    getNameList(){
       this.loading = true;
         listSportsmanName(this.queryParams).then(response => {
           this.sportsmanNameList = response.rows;
           this.total = response.total;
           this.loading = false;
         });

    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        sportsmanId: undefined,
        matchName: undefined,
        matchTime: undefined,
        venue: undefined,
        event: undefined,
        achievement: undefined,
        memo: undefined,
        imgUrl: '',
        ctime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加赛事成绩";
      this.getNameList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids

      getSportsmanMatch(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改赛事成绩";
        this.getNameList();
      });
    },
    evidenceBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 10
      if (!isRightSize) {
        this.$message.error('文件大小超过 10MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
    evidenceUploadSuccess(res, file) {
      // 如果上传成功
      if (res.code == 200) {
        this.form.imgUrl += res.url;
        this.form.imgUrl += ";";
      } else {
        this.$message.error("图片插入失败");
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSportsmanMatch(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addSportsmanMatch(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除sportsmanMatch编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSportsmanMatch(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有sportsmanMatch数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSportsmanMatch(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>